// 导入
const fs = require('fs')
const path = require('path')
const ServerConfig = require('../server.config.js')
const banners = require('../banners.json')
const { get } = require('http')
const { getDefaultSettings } = require('http2')

/**
 * 读取路径下对应的文件
 * @param {具体的文件目录} pathD 
 * @returns 返回文件数据内容
 */
async function readFile(pathD){
    return new Promise((success,error)=>{
        fs.readFile(pathD, 'utf8', (err, data) => {
            if (err) {
              console.log(err)
              return
            }
            success(data)
          })
    })
}
/**
 * 保存路径下的文件
 * @param {具体的文件目录} pathD 
 * @returns 返回文件数据内容
 */
async function writeFile(pathD,content){
    return new Promise((success,error)=>{
        fs.writeFile(pathD, content, (err, data) => {
            if (err) {
              console.log(err)
              return
            }
            success('ok')
          })
    })
}
module.exports = {
    /**
     * 获得一个Promise,通过它获得轮播图数据字符串
     * @returns 
     */
    async getBanners(){
        return readFile(path.join(__dirname,"../","banners.json"))
    },
    async getDisCountBanners(){
        return readFile(path.join(__dirname,"../","discount.json"))
    },
    async getHotGoodsBanners(){
        return readFile(path.join(__dirname,"../","hotGood.json"))
    },
    async saveSipper(content){
        return writeFile(path.join(__dirname,'../','banners.json'),content)
    },
    async saveHot(content){
        return writeFile(path.join(__dirname,'../','hotGood.json'),content)
    },
    async saveDiscount(content){
        return writeFile(path.join(__dirname,'../','discount.json'),content)
    },
}